package com.glory.brepda.activity.lanjian;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.glory.brepda.R;
import com.glory.brepda.adapter.BreMhCheckLotAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.BreMhCheckLotEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;
import butterknife.BindView;
import butterknife.OnClick;

public class BreMhCheckLotActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etCutterLot)
    FreeUI_ClearEditText etCutterLot;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    private BreMhCheckLotAdapter mAdapter;
    private FreeUI_GeneralPop markPop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_mhchecklot;
    }

    @Override
    protected void initView() {
        tvBaseSubmit.setVisibility(View.GONE);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        initRecyclerView(rvLotList, this);
        mAdapter = new BreMhCheckLotAdapter(null);
        mAdapter.openLoadAnimation(BreMhCheckLotAdapter.ALPHAIN);
        mAdapter.isFirstOnly(false);
        rvLotList.setAdapter(mAdapter);

        initEdit(etLot, s -> {
            //获取lot信息
            LotUtils.searchLotInfo(BreMhCheckLotActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    etCutterLot.requestFocus();
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }
            });
        });

        initEdit(etCutterLot, s -> {
            submit();
        });
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit();
                break;
        }
    }

    private void submit(){

        if(getText(etLot).equals("")){
            showToast("请先扫描批次信息", false);
            return;
        }else if(getText(etCutterLot).equals("")){
            showToast("请先扫描标签二维码信息", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<BreMhCheckLotEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<BreMhCheckLotEntity>>
                (new TypeToken<BaseResponseBean<BreMhCheckLotEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<BreMhCheckLotEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    if(response.body().getResponse().getBody().getResult().getReserved2() == null ||
                            response.body().getResponse().getBody().getResult().getReserved2().equals("Y")){
                        showToast("保存成功", true);
                        mAdapter.addData(0, response.body().getResponse().getBody().getResult());
                    }
                    clean();
                    etLot.requestFocus();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BreMhCheckLotEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDA_WIP_LOT_SUBMISSIONHIS");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOT_ID", getText(etLot));
        map.addProperty("CUTTER_LOT_ID", getText(etCutterLot));
        NetUtils.requestPostNet("IF.PDA_MH_CHECK_LOT", "Common", BreMhCheckLotActivity.this, map, callBack);
    }

    public void clean(){
        etCutterLot.setText("");
    }

    @Override
    protected void initData() {

    }

}